//package com.cqeec.demo;
//
//import com.cqeec.demo.entity.Cat;
//import com.cqeec.demo.entity.Dog;
//import com.cqeec.demo.entity.Person;
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.context.ConfigurableApplicationContext;
//import org.springframework.context.annotation.Bean;
//
//import java.util.Map;
//
///**
// * 主程序入口
// */
//@SpringBootApplication
//public class DemoApplication {
//    public static void main(String[] args) {
//        //应用的上下文
//        ConfigurableApplicationContext ioc = SpringApplication.run(DemoApplication.class, args);
//        System.out.println("===========容器创建完成===========");
//        Cat tom1=(Cat) ioc.getBean("tom");
//        System.out.println(tom1);
//        Cat tom2=(Cat) ioc.getBean("tom");
//        System.out.println(tom2);
//        Cat tom3=(Cat) ioc.getBean("tom");
//        System.out.println(tom3);
//
//        Person zs=ioc.getBean("zs",Person.class);
//        Cat cat=ioc.getBean("cat",Cat.class);
//        System.out.println(zs);
//        System.out.println(cat);
//    }
//    @Bean("tom")
//    public Cat cat(){
//        Cat cat=new Cat();
//        cat.setName("tom");
//        cat.setAge(3);
//        return cat;
//    }
//    public static void test02(String[] args) {
//        //应用的上下文
//        ConfigurableApplicationContext ioc = SpringApplication.run(DemoApplication.class, args);
//        //获取容器中的组件
//        //1.根据名字获取
//        //NosuchBeanDefinitionException 组件中没有这个名字的组件
//        //名字：@Bean后没有名字时 方法名就是名字，有名字时@Bean（这个就是名字）
//        Person lisi=(Person)ioc.getBean("haha");
//        System.out.println(lisi);
//        //2.根据类型获取
//        //NoUniqueBeanDefinitionException 没有唯一的类型的组件
////		Person bean = ioc.getBean(Person.class);
////		System.out.println(bean);
//        //3.获取这个类型的所有组件
//        Map<String,Person> beanOfType=ioc.getBeansOfType(Person.class);
//        System.out.println(beanOfType);
//        //4.根据名字+类型获取
//        Person haha=ioc.getBean("haha",Person.class);
//        System.out.println(haha);
//    }
//    @Bean("haha")
//    public Person zhaoliu(){
//        return new Person ("赵六",21,2);
//    }
//
//    public static void test01(String[] args) {
//        //应用的上下文
//        ConfigurableApplicationContext ioc = SpringApplication.run(DemoApplication.class, args);
//        //获取所有组件
//        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
//        //遍历组件
//        for (String beanDefinitionName : beanDefinitionNames) {
//            System.out.println(beanDefinitionName);
//        }
//        Person person =new Person("zhuangsan", 12, 1);
//        person.setAge(122);
//        System.out.println(person);
//        System.out.println(new Person("张三",20,1));
//
//    }
//    @Bean
//    public Person zhangsan(){
//        return new Person("张三",19,1);
//    }
//    @Bean
//    public  Person wangwu(){
//        return new Person("王五",15,0);
//    }
//
//    @Bean
//    public Dog dog() {
//        return new Dog("Buddy", 3);
//    }
//}






package com.cqeec.demo;

import ch.qos.logback.core.LifeCycleManager;
import com.cqeec.demo.controller.BookController;
import com.cqeec.demo.dao.BookDao;
import com.cqeec.demo.entity.Book;
import com.cqeec.demo.service.BookService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

import java.util.Map;

@SpringBootApplication
@Import(LifeCycleManager.class)
//@ComponentScan(basePackages="com.cqeec.demo.controller")
public class DemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext ioc=SpringApplication.run(DemoApplication.class, args);
        Book book = ioc.getBean(Book.class);
        System.out.println(book);
        System.out.println("======容器创建完成======");
        //获取bookController层的组件
        BookController bean=ioc.getBean(BookController.class);
        System.out.println(bean);
        // 获取bookService层的组件
        BookService bookServiceBean = ioc.getBean(BookService.class);
        System.out.println(bookServiceBean);

        // 获取bookDao层的组件
        BookDao bookDaoBean = ioc.getBean(BookDao.class);
        System.out.println(bookDaoBean);


        //根据类型获取   ,----报错
        Book bean0=ioc.getBean(Book.class);
        //根据名字获取
        Book bean1=(Book)ioc.getBean("red");
        //根据名字+类型获取
        Book bean2=(Book)ioc.getBean("red",Book.class);
        //获取这个类型的所有组件
        Map<String,Book> bean3=ioc.getBeansOfType(Book.class);
    }
}